package com.hfut.eeg.service;

import com.hfut.eeg.common.component.EEGcache;
import com.hfut.eeg.common.util.BlobListTrans;
import com.hfut.eeg.common.util.EEGUtil;
import com.hfut.eeg.dao.BrainFFTMapper;
import com.hfut.eeg.dto.EEG;
import com.hfut.eeg.dto.EEGTrans;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author 梁琦
 * @program: eeg
 * @Package com.hfut.eeg.service
 * @Description: TODO
 * @date 2019-12-11 10:23
 */
@Service
@Scope("singleton")
public class BrainFFTService {

    private static final Logger LOGGER = LoggerFactory.getLogger(BrainFFTService.class);
    @Autowired
    private BrainFFTMapper brainFFTMapper;

    @Autowired
    private EEGcache eeGcache;

    /**
    * @Description: 分段获取FFT数据
    * @param eegId 病人ID order 开始数据段位置  num数据段数量
    * @return List<EEGTrans> 数据数组
    * @author 梁琦
    * @date 2019-12-30 20:13
    */
    public List<EEGTrans> getBrainFFT(String eegId, int order,int num){
        HashMap<String,Object> getReq = new HashMap<>();
        getReq.put("eegId",eegId);
        ArrayList<Integer> list = new ArrayList<>();
        for (int i=order;i<order+num;i++){
            list.add(i);
        }
        getReq.put("sequence",list);
        return brainFFTMapper.getBrainFFT(getReq);
    }

    /**
    * 获取单个FFT数据
    */
    public EEGTrans getBrainFFTSingle(HashMap<String,Object> getReq){
        String eegId = (String) getReq.get("eegId");
        Integer order = (Integer) getReq.get("sequence");
        List<EEGTrans> list = getBrainFFT(eegId,order,1);
        //有效性检验
        if(list==null || list.size()==0){
            return null;
        }
        return list.get(0);
    }

    /**
    * @Description: 利用数组插入FFT数据
    * @param cache FFT数据数组 eegId 病人ID
    * @return
    * @author 梁琦
    * @date 2019-12-30 20:14
    * @throws
    */
    public int insertFFTByArray(ArrayList<ArrayList>cache,String eegId){
        //数据有效性检验
        if(cache==null || cache.size()<16 || eegId==null){
            LOGGER.error("插入FFT表时未通过数据检验");
            return -1;
        }
        if(cache.get(0).size()<500){
            LOGGER.error("FFT数据量小于1s");
            return -1;
        }

        //获取当前数据段
        int order = getBrainFFTLatestSequence(eegId);
        int storeSize = 0;

        HashMap<String,Object> getReq = new HashMap<>();
        getReq.put("eegId",eegId);
        getReq.put("sequence",order);
        EEGTrans trans = getBrainFFTSingle(getReq);

        HashMap<String,Object> map = new HashMap<>();
        String []labels = eeGcache.getLabels();

        //解析blob数据，确定数据库中存储数据的数据量
        ArrayList<ArrayList> stores = BlobListTrans.transEEGTransToList(trans);
        if(stores==null){
            stores = new ArrayList<>();
            storeSize  =-1;
        }else {
            storeSize = stores.get(0).size()/500;
        }

        //表示数据库中没有相关信息，要插入数据库
        if(storeSize >= 10 || storeSize<0){
            for (int i = 0; i<eeGcache.getNchannel(); i++){
                map.put(labels[i],cache.get(i));
            }
            map.put("eegId",eegId);
            map.put("Id",System.currentTimeMillis());
            map.put("sequence",order+1);
            ArrayList list = (ArrayList) map.get(labels[0]);
            LOGGER.info("FFT此时向数据库插入的数据量："+list.size());
            return brainFFTMapper.insertBrainFFT(map);
        }else {
            //更新数据库
            for (int i = 0; i<eeGcache.getNchannel(); i++){
                stores.get(i).addAll(cache.get(i));
                map.put(labels[i],stores.get(i));
            }
            map.put("eegId",eegId);
            map.put("sequence",order);
            ArrayList list = (ArrayList) map.get(labels[0]);
            LOGGER.info("FFT此时向数据库更新的数据量: "+list.size());
            return brainFFTMapper.updateBrainFFT(map);
        }
    }

    /**
    * @Description: 获取分段FFT数据
    * @param eegId 病人ID order 开始数据段位置  num数据段数量
    * @return ArrayList<ArrayList> FFT结果
    * @author 梁琦
    * @date 2019-12-30 20:15
    * @throws
    */
    public ArrayList<ArrayList> getFFTBySeg(String eegId,int order,int num){
        List<EEGTrans> eeg = getBrainFFT(eegId,order,num);
        if(eeg==null){
            return null;
        }
        ArrayList<ArrayList> result = new ArrayList<>();
        for (int i=0;i<16;i++){
            result.add(new ArrayList());
        }
        for (EEGTrans trans : eeg) {
            ArrayList<ArrayList> lists = BlobListTrans.transEEGTransToList(trans);
            for (int j = 0; lists != null&&j < lists.size(); j++) {
                result.get(j).addAll(lists.get(j));
            }
        }
        return result;
    }

    /**
    * 获取当前病人最新FFT序号
    */
    public int getBrainFFTLatestSequence(String eegId){
        Integer sequence = brainFFTMapper.getBrainFFTLatestSequence(eegId);
        return sequence==null?0:sequence;
    }

    public int insertFFTList(List<EEGTrans> brainFFT){
        return brainFFTMapper.insertFFTList(brainFFT);
    }
}
